<?php

include('pagina.php');
include('database.php');
include('auth.php');
include('core.php');

// Verifica se esta autenticado com pelo menos nivel 3
validaPermissao(3);

// Verifica se ha income para ver
if (!$_GET['id'])
{
	forbidden();
}

// Busca todos os dados do income
$period_income = query_fetch("SELECT * FROM period_duedates WHERE id={$_GET['id']} AND value > 0 LIMIT 1");

// Se nao existe, cai fora
if (!$period_income)
{
	forbidden();
}

if ($_GET['cancel'] == 1)
{
	if ($income['references_to'] == CONTACT)
		query_bd("DELETE FROM contacts WHERE id={$period_income['reference_id']} LIMIT 1");

	query_bd("DELETE FROM period_duedates WHERE id={$period_income['id']} LIMIT 1");
	header( "Location: incomes_expenses.php?msg=The previous period income was excluded from the system." );
	exit;
}

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - Period Income');
pagina_head();

// Opcoes
echo "<ul id=opcoes>";
echo "<li><a href='period_income.php?id={$period_income['id']}&cancel=1' ",
"onClick='return confirm(\"Do you really want to cancel this period income?\");'>",
"<img src='img/cancel.png' />Cancel period income(exclude)</a></li>";
echo "<li><a href='period_income.php?id={$_GET['id']}&print=1' target='_blank' ><img src='img/print.png' />Print</a></li>";
echo "</ul><br>";

// Mostra dados do income
echo "<ul class=dados_estaticos>";
echo "<h1>Period Income</h1><br>";
echo "<li><strong>Name:</strong> {$period_income['name']}</li>";
echo "<li><strong>Description:</strong> {$period_income['description']}</li>";
echo "<li><strong>Category:</strong> ", $INC_CATEGORIES[$period_income['category']], "</li>";
echo "<li><strong>Period:</strong> ", $DUEDATESPERIODS[$period_income['period']], "</li>";
echo "<li><strong>Next due date:</strong> ",
	date("M j Y", strtotime($period_income['next_duedate'])), "</li>";
echo "<li><strong>Value:</strong> " . show_money($period_income['value']), "</li>";

switch ($period_income['references_to'])
{
	case EMPLOYEE:
		$employee = query_fetch("SELECT employees.id, contacts.name FROM employees, contacts WHERE " .
			"employees.id={$period_income['reference_id']} AND contacts.id=employees.contact_id LIMIT 1");
		echo "<li><strong>Employee:</strong> <a href='employee.php?id={$employee['id']}'>{$employee['name']}</a></li>";
		break;
	case CUSTOMER:
		$customer = query_fetch("SELECT customers.id, contacts.name FROM customers, contacts WHERE " .
			"customers.id={$period_income['reference_id']} AND contacts.id=customers.contact_id LIMIT 1");
		echo "<li><strong>Customer:</strong> <a href='customer.php?id={$customer['id']}'>{$customer['name']}</a></li>";
		break;
	case LOCATION:
		$location = query_fetch("SELECT locations.id, contacts.name FROM locations, contacts WHERE " .
			"locations.id={$period_income['reference_id']} AND contacts.id=locations.locationcontact LIMIT 1");
		echo "<li><strong>Location:</strong> <a href='location.php?id={$location['id']}'>{$location['name']}</a></li>";
		break;
	case REPORT:
		$report = query_fetch("SELECT id FROM reports WHERE id={$period_income['reference_id']}");
		echo "<li><strong>Report:</strong> <a href='report.php?id={$report['id']}'><img src='img/report.png'/></a></li>";
		break;
	case EVENT:
		$event = query_fetch("SELECT reports.id, events.name FROM events, reports WHERE " .
			"reports.id={$period_income['reference_id']} AND reports.special_event=FALSE AND events.id=reports.location_event_id LIMIT 1");
		echo "<li><strong>Event:</strong> <a href='report.php?id={$event['id']}'>{$event['name']}</a></li>";
		break;
	case CONTACT:
		$contact = query_fetch("SELECT * FROM contacts WHERE id={$period_income['reference_id']} LIMIT 1");
		echo "<hr><h2>Contact</h2><br>";
		echo "<li><strong>Address:</strong> ",
		"{$contact['number']}, {$contact['street']}, {$contact['complement']} ",
		"zip: {$contact['zipcode']}, {$contact['city']} - {$contact['state']}.</li>";
		if ($contact['phone'])
			echo "<li><strong>Phone number:</strong> {$contact['phone']}</li>";
		if ($contact['cellphone'])
			echo "<li><strong>Mobile number:</strong> {$contact['cellphone']}</li>";
		if ($contact['email'])
			echo "<li><strong>Email:</strong> {$contact['email']}</li>";
		break;
}

if ($income['doc_number'])
	echo "<li><strong>Document number:</strong> {$period_income['doc_number']}</li>";
if ($income['obs'])
	echo "<li><strong>Observations:</strong> {$period_income['obs']}</li>";

echo "</ul>";

// Finaliza pagina
pagina_fim();
?>
